package com.modakai.med.cmn.controller;

import com.modakai.med.cmn.service.DictService;
import com.modakai.med.common.result.Result;
import com.modakai.med.model.cmn.Dict;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Tag(name = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService dictService;

    @Operation(summary = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode) {
        return Result.ok(dictService.findByDictCode(dictCode));
    }

    // 根据父级Code和值获取字典名称
    @Operation(summary = "根据父级Code和值获取字典名称", parameters = {
            @Parameter(name = "parentCode", description = "父级Code", required = true),
            @Parameter(name = "value", description = "字典值", required = true)
    })
    @GetMapping("getName/{parentCode}/{value}")
    public Result<String> getName(@PathVariable String parentCode,
                          @PathVariable String value) {
        return Result.ok(dictService.getNameByParentCodeValue(parentCode, value));
    }


    // 根据值获取字典名称
    @Operation(summary = "根据值获取字典名称", parameters = {
            @Parameter(name = "value", description = "字典值", required = true)
    })
    @GetMapping("getName/{value}")
    public Result<String> getName(@PathVariable String value) {
        return Result.ok(dictService.getNameByParentCodeValue(null, value));
    }

    @Operation(summary = "根据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable Long id) {
        return Result.ok(dictService.findChildData(id));
    }

    @Operation(summary = "导出字典数据")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @Operation(summary = "导入")
    @PostMapping("importData")
    public Result<String> importData(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

}
